package com.tentative.competition.common.utils;

import java.io.File;
import java.util.Map;
import java.util.HashMap;
import java.io.FileOutputStream;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.util.BytePictureUtils;
import com.deepoove.poi.data.PictureRenderData;
import com.tentative.competition.common.core.domain.model.TentativeWordVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class InsertWordUtil {

    @Value(value = "${tentative-competition.word-template-url}")
    private String templateUrl;

    @Value(value = "${tentative-competition.word-template-name}")
    private String templateName;


    /**
     * 生成作品word
     * @param wordVo
     * @return
     */
    public String insertTentativeWord(TentativeWordVo wordVo){
        String resultUrl = templateUrl + "\\\\" + wordVo.getId() + "\\\\" + wordVo.getFileName();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("title", wordVo.getTitle());
            map.put("teamName", wordVo.getTeamName());
            map.put("guidanceName", wordVo.getGuidanceName());
            map.put("unveilUniversity", wordVo.getUnveilUniversity());
            map.put("synopsis", wordVo.getSynopsis());
            // 读取本地磁盘图片
            map.put("pic", new PictureRenderData(600, 600, wordVo.getPic()));
            // 通过url读取网络图片
//        map.put("picture", new PictureRenderData(200, 400, ".png", BytePictureUtils.getUrlByteArray("https://res.wx.qq.com/a/wx_fed/weixin_portal/res/static/img/1EtCRvm.png")));
            File file = new File(templateUrl + templateName);
            XWPFTemplate template = XWPFTemplate.compile(file).render(map);
            FileOutputStream out = new FileOutputStream(new File(resultUrl));
            template.write(out);
            out.flush();
            out.close();
            template.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultUrl;
    }

    public static void main(String[] args) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("worksName", "“微舞”");
        map.put("teamName", "王嘉瑛、陈盼、周昕儿");
        map.put("guidanceName", "胡思顺");
        map.put("unveilUniversity", "华中农业大学");
        map.put("synopsis", "本组作品以灿烂中华文化为核心，以中国传统艺术表演:舞龙、舞狮、京剧结合微生物展现舞动之美。突出传统艺术与生命的交织，让优秀文化在新时代迸发无穷生命力。经悉心设计，用灭菌移液枪枪头为画笔，以粘质沙雷氏菌、大肠杆菌、金黄色葡萄球菌为颜料，在LB培养基上完成。");
        // 读取本地磁盘图片
        map.put("pic", new PictureRenderData(600, 600, "D:\\L项目\\t1.jpg"));
        // 通过url读取网络图片
//        map.put("picture", new PictureRenderData(200, 400, ".png", BytePictureUtils.getUrlByteArray("https://res.wx.qq.com/a/wx_fed/weixin_portal/res/static/img/1EtCRvm.png")));
        File file = new File("D:\\L项目\\生物大赛作品模板.docx");
        XWPFTemplate template = XWPFTemplate.compile(file).render(map);
        FileOutputStream out = new FileOutputStream(new File("D:\\L项目\\new.docx"));
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }

}
